﻿$ErrorState = 2
$ErrorMessage = ""
$Trace = ""
$Value = ""
$Error.Clear()

$virstoNamespace = "root\virtualization\storage"
$virstoServerClass = "Virsto_VSServer"
$hostid = ""
try
{
    $Trace += 'Connecting to Virsto...  \n'

	$virstoServer = Get-WmiObject  -Namespace $virstoNamespace -Class $virstoServerClass

	if ($virstoServer -eq $null)
	{
		$ErrorState = 1
		$Trace += 'The Virsto Master Service not run on this box...  \n'

		$Trace += 'Check if this is a cluster...  \n'

		$Query = "select GroupComponent from MSCluster_NodeToActiveGroup where PartComponent=`"MSCluster_ResourceGroup.Name='Virsto Master Server'`""
		$node = gwmi -query $Query -namespace "root\MSCluster" | Select-Object GroupComponent

		if ($node  -eq $null)
		{

			$ErrorState = 1
			$Trace +=' This is no cluster or Master Service not found on the Cluster Servics...  \n'
		}
		else
		{

			$ErrorState = 0
			$Trace += 'Cluster Service found...  \n'

			$hostid = $node.GroupComponent

		}


    }
    else
    {
		$ErrorState = 0
		$Trace += 'Master Service found...  \n'
		$hostid = 'HOSTNAME="' + $virstoServer.HostName + '"'

	}




}
catch {
    $ErrorState = 2
    $ErrorMessage = $error[0].Exception.ToString()
}
Finally {
    $Trace += 'ErrorState:  ' + $ErrorState + ' \n'
    $Trace += 'ErrorMessage: ' + $ErrorMessage + ' \n'
    $Trace += 'Virsto Master Server: ' + $hostid + ' \n'
	
}

	$Results = @($ErrorState, $ErrorMessage, $Trace, $hostid) 
	Return $Results
